package com.yangmin.rabbit.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



/**
 * @author diandian
 * @date 2021-12-11 18:33
 */
@Configuration
public class RabbitConfig {
    public static final  String SIMPLE_QUEUE="simple_queue";
    /**
     *    手动创建queue消息队列
     *    方法会判断  有没有simple_queue消息队列  如果有就不创建  如果没有就先创建再进行消息的存储
     */
    @Bean
    public Queue simpleQueue(){
        return new Queue(SIMPLE_QUEUE);
    }


    /************广播模式*****************/
    public static final String FANOUT_EXCHANGE = "fantout.exchange";

    public static final String FANOUT_QUEUE1 = "fantout.queue1";

    public static final String FANOUT_QUEUE2 = "fantout.queue2";

    @Bean
    public FanoutExchange fantoutExchange(){
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Queue fanoutQueue1(){
        return new Queue(FANOUT_QUEUE1);
    }

    @Bean
    public Queue fanoutQueue2(){
        return new Queue(FANOUT_QUEUE2);
    }

    //建立交换机与队列的直接绑定关系
    @Bean
    public Binding bindFanoutQueue1(Queue fanoutQueue1, FanoutExchange fantoutExchange){
        return BindingBuilder.bind(fanoutQueue1).to(fantoutExchange);
    }


    @Bean
    public Binding bindFanoutQueue2(Queue fanoutQueue2,FanoutExchange fantoutExchange){
        return BindingBuilder.bind(fanoutQueue2).to(fantoutExchange);
    }

    /************路由模式*****************/
    public static final String DIRECT_EXCHANGE = "direct.exchange";

    public static final String DIRECT_ROUTEKEY_EMAIL = "dicret.email";

    public static final String DIRECT_ROUTEKEY_SM = "direct.sm";

    public static final String DIRECT_QUEUE_EMAIL = "direct.queue.email";

    public static final String DIRECT_QUEUE_SM = "direct.queue.sm";

    @Bean
    public Queue directEmailQueue(){
        return new Queue(DIRECT_QUEUE_EMAIL);
    }

    @Bean
    public Queue directSmQueue(){
        return new Queue(DIRECT_QUEUE_SM);
    }
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }
    // 建立队列与交换机之间的联系
    @Bean
    public Binding bingingEmailQueue(Queue directEmailQueue,DirectExchange directExchange ){
        return BindingBuilder.bind(directEmailQueue).to(directExchange).with(DIRECT_ROUTEKEY_EMAIL);
    }

    @Bean
    public Binding bingingSmQueue(Queue directSmQueue,DirectExchange directExchange ){
        return BindingBuilder.bind(directSmQueue).to(directExchange).with(DIRECT_ROUTEKEY_SM);
    }

    /************主题模式*****************/
    public static final String TOPIC_EXCHANGE = "topic.exchange";

    public static final String TOPIC_ROUTEKEY1 = "brand.*";

    public static final String TOPIC_ROUTEKEY2 = "brand.#";

    public static final String TOPIC_QUEUE1 = "topic.queue1";

    public static final String TOPIC_QUEUE2 = "topic.queue2";

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Queue topicQueue1(){
        return new Queue(TOPIC_QUEUE1);
    }

    @Bean
    public Queue topicQueue2(){
        return new Queue(TOPIC_QUEUE2);
    }

    @Bean
    public Binding bindingTopicQueue1(Queue topicQueue1,TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with(TOPIC_ROUTEKEY1);
    }

    @Bean
    public Binding bindingTopicQueue2(Queue topicQueue2,TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with(TOPIC_ROUTEKEY2);
    }









}
